package com.collection;

import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class ListDemo {
    public List<String> getList(List<String> list) {
        int listSize = 3000;
        for (int i = 0; i < listSize; i++) {
            list.add(SecureRandomStringGenerator.generateRandomString(10));
        }
        return list;
    }

    public void loopList() {
        List<String> list = getList(new ArrayList<>());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (String s : list) {
            s.toString();
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
        List<String> list2 = getList(new LinkedList<>());
        stopWatch.start();
        for (String s : list2) {
            s.toString();
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }

    public void loopLinkedList() {
        List<String> list = getList(new LinkedList<>());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (String s : list) {
            s.toString();
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }

    public void accessArrayList(){
        List<String> list1 = getList(new ArrayList<>());
        List<String> list2 = getList(new LinkedList<>());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < 100; i++) {
            list1.get(new Random().nextInt(list1.size()));
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());

        stopWatch.start();
        for (int i = 0; i < 100; i++) {
            list2.get(new Random().nextInt(list1.size()));
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }
}
